package edu.luas.digitalmedia.controller;

import edu.luas.digitalmedia.pojo.MyAddress;
import edu.luas.digitalmedia.service.MyAddressService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/address/insert")
public class MyAddressInsertController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1:设置请求编码
        req.setCharacterEncoding("utf-8");
        //2:设置响应编码
        resp.setContentType("text/html;charset=utf-8");
        //3:获取请求参数
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String area = req.getParameter("area")+req.getParameter("areaa")+req.getParameter("areab");
        String area1 = req.getParameter("area1");
        String types = req.getParameter("types");

        MyAddress model = new MyAddress();
        model.setName(name);
        model.setPhone(phone);
        model.setArea(area);
        model.setArea1(area1);
        model.setTypes(types);

        //4:调用业务方法处理请求
        try {
            int count = new MyAddressService().insert(model);
            if (count>0){
                resp.getWriter().print("添加成功");
            }else {
                resp.getWriter().print("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().print("添加失败");
        }
        //5:控制页面跳转
        req.getRequestDispatcher("/address/list").forward(req,resp);
    }
}
